package com.jstyle.beatecg.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jstyle.beatecg.R;
import com.jstyle.beatecg.adapter.SportViewPagerAdapter;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.Disposable;
import lecho.lib.hellocharts.model.LineChartData;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ActivityFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ActivityFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    Unbinder unbinder;
    @BindView(R.id.Chronometer_activity)
    TextView ChronometerActivity;
    @BindView(R.id.tv_heart_data)
    TextView tvHeartData;
    @BindView(R.id.tv_step_data)
    TextView tvStepData;
    @BindView(R.id.tv_distance_data)
    TextView tvDistanceData;
    @BindView(R.id.tv_cal_data)
    TextView tvCalData;
    @BindView(R.id.viewPager_activity)
    ViewPager viewPagerActivity;
    @BindView(R.id.rg_home)
    RadioGroup rgHome;
    private LineChartData data;
    private int numberOfLines = 1;
    private int maxNumberOfLines = 4;
    private int numberOfPoints = 20;
    private float[][] randomNumbersTab = new float[maxNumberOfLines][numberOfPoints];

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;
    private Disposable disposable;
    LinkedList<Float> queueHeart = new LinkedList<>();
    private SportViewPagerAdapter viewPagerAdapter;

    public ActivityFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment ActivityFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ActivityFragment newInstance() {
        ActivityFragment fragment = new ActivityFragment();

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_activity, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    int countTime;

    private void initView() {
        viewPagerAdapter = new SportViewPagerAdapter(getChildFragmentManager());
        viewPagerActivity.setAdapter(viewPagerAdapter);
        viewPagerActivity.setCurrentItem(0);
        viewPagerActivity.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    rgHome.check(R.id.rb_home_0);
                } else {
                    rgHome.check(R.id.rb_home_1);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void updateActivityData(float cal, int distance) {
        tvCalData.setText(cal + "");
        tvDistanceData.setText(distance/100f + "");
    }

    public void updateSpeed(float speed) {
        NumberFormat numberFormat=NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(1);
        tvStepData.setText(numberFormat.format(speed));
        ChartFragment chartFragment = (ChartFragment) viewPagerAdapter.getItem(0);
        chartFragment.updateData(speed, heart);

    }

    float heart;

    public void updateHeartRate(float heartRate) {
        tvHeartData.setText(heartRate + "");
        heart = heartRate;
//        if (queueHeart.size() > numberOfPoints) queueHeart.poll();
//        queueHeart.offer(heartRate);

    }

    public void updateTime(String time) {
        ChronometerActivity.setText(time);
    }

//    private void generateValues() {
//        for (int i = 0; i < maxNumberOfLines; ++i) {
//            for (int j = 0; j < numberOfPoints; ++j) {
//                randomNumbersTab[i][j] = (float) Math.random() * 50f + 5;
//            }
//        }
//    }


    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
//        if (context instanceof OnFragmentInteractionListener) {
//            mListener = (OnFragmentInteractionListener) context;
//        } else {
//            throw new RuntimeException(context.toString()
//                    + " must implement OnFragmentInteractionListener");
//        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
